package com.gscitysfy.cus.drain.modules.resource.convert;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gscitysfy.cloudfrmwk.assistant.domain.Result;
import com.gscitysfy.cloudfrmwk.portal.feign.DistrictService;
import com.gscitysfy.cus.drain.modules.command.entity.DangerSituationManage;
import com.gscitysfy.cus.drain.modules.command.entity.InstructTeam;
import com.gscitysfy.cus.drain.modules.command.mapper.InstructTeamMapper;
import com.gscitysfy.cus.drain.modules.resource.dto.ResourceTeamDto;
import com.gscitysfy.cus.drain.modules.resource.entity.ResourceTeam;
import com.gscitysfy.cus.drain.utils.CoordinateUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author jiang_wen
 * @Date 2022/10/23 16:35
 * @Description TODO
 */
@AllArgsConstructor
@Component
public class ResourceTeamConvert {

    private final DistrictService districtService;

    private final InstructTeamMapper instructTeamMapper;

    public ResourceTeam toEntity(ResourceTeam resourceTeam, ResourceTeamDto resourceTeamDto) {
        resourceTeam.setManagePerson(resourceTeamDto.getManagePerson());
        resourceTeam.setPhone(resourceTeamDto.getPhone());
        resourceTeam.setAddress(resourceTeamDto.getAddress());
        resourceTeam.setLgtd(resourceTeamDto.getLgtd());
        resourceTeam.setLttd(resourceTeamDto.getLttd());
        resourceTeam.setPersonCount(resourceTeamDto.getPersonCount());
        resourceTeam.setRemark(resourceTeamDto.getRemark());
        resourceTeam.setTeamName(resourceTeamDto.getTeamName());
        resourceTeam.setSimNo(resourceTeamDto.getSimNo());
        resourceTeam.setGps(resourceTeamDto.getGps());
        resourceTeam.setGpsCode(resourceTeamDto.getGpsCode());
        resourceTeam.setCmRegflag(resourceTeamDto.getCmRegflag());
        return resourceTeam;
    }

    public ResourceTeamDto toDto(ResourceTeam resourceTeam, DangerSituationManage dangerSituationManage) {
        ResourceTeamDto resourceTeamDto = new ResourceTeamDto();
        resourceTeamDto.setCmKid(resourceTeam.getCmKid());
        resourceTeamDto.setTeamName(resourceTeam.getTeamName());
        resourceTeamDto.setPersonCount(resourceTeam.getPersonCount());
        resourceTeamDto.setPhone(resourceTeam.getPhone());

        //计算经纬度
        BigDecimal startXPoint = resourceTeam.getLgtd();
        BigDecimal startYPoint = resourceTeam.getLttd();
        BigDecimal endXPoint = dangerSituationManage.getLongitude();
        BigDecimal endYPoint = dangerSituationManage.getLatitude();
        BigDecimal distance = CoordinateUtil.getDistance(startXPoint, startYPoint, endXPoint, endYPoint);
        resourceTeamDto.setDistance(distance.doubleValue());

        LambdaQueryWrapper<InstructTeam> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(InstructTeam::getTeamId, resourceTeam.getCmKid());
        queryWrapper.eq(InstructTeam::getStatus, "ACCEPT");
        queryWrapper.eq(InstructTeam::getCmUsable, false);
        List<InstructTeam> instructTeamList = instructTeamMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(instructTeamList)) {
            resourceTeamDto.setAssignedFlag(true);
        }else {
            resourceTeamDto.setAssignedFlag(false);
        }
        return resourceTeamDto;
    }

    public ResourceTeamDto toDto(ResourceTeam resourceTeam) {
        ResourceTeamDto resourceTeamDto = new ResourceTeamDto();
        if(resourceTeam!=null){
            resourceTeamDto.setCmKid(resourceTeam.getCmKid());
            resourceTeamDto.setManagePerson(resourceTeam.getManagePerson());
            resourceTeamDto.setPhone(resourceTeam.getPhone());
            resourceTeamDto.setAddress(resourceTeam.getAddress());
            resourceTeamDto.setLgtd(resourceTeam.getLgtd());
            resourceTeamDto.setLttd(resourceTeam.getLttd());
            resourceTeamDto.setPersonCount(resourceTeam.getPersonCount());
            resourceTeamDto.setRemark(resourceTeam.getRemark());
            resourceTeamDto.setTeamName(resourceTeam.getTeamName());
            resourceTeamDto.setSimNo(resourceTeam.getSimNo());
            resourceTeamDto.setGps(resourceTeam.getGps());
            resourceTeamDto.setGpsCode(resourceTeam.getGpsCode());
            resourceTeamDto.setCmRegflag(resourceTeam.getCmRegflag());
            if (Objects.nonNull(resourceTeam.getCmRegflag())) {
                Result<String> result = districtService.getDistrictNameByCode(String.valueOf(resourceTeam.getCmRegflag()));
                if (result.getStatus() == 200) {
                    resourceTeamDto.setCmRegflagName(result.getData());
                }
            }
            resourceTeamDto.setAssignedFlag(resourceTeam.getAssignedFlag());
            return resourceTeamDto;
        }
        return  null;
    }

    public List<ResourceTeamDto> toDtoList(List<ResourceTeam> resourceTeamList) {
        if (CollectionUtils.isEmpty(resourceTeamList)) {
            return Collections.emptyList();
        }
        return resourceTeamList.stream().map(this::toDto).collect(Collectors.toList());
    }

}
